
package apibasej.client;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import apibasej.shared.config.ConfigConsumer;

/**
 * http://code.google.com/p/apibasej/
 * 
 * @author Julio Azevedo Lessa - apibasej@gmail.com
 * 
 */
public class FileUp extends ConfigConsumer{

	private String name = null;
	private File file = null;
	private String contentType = null;
	private String uploadDirectory = null;
	private String uRLEncodedUploadName;

	@SuppressWarnings("deprecation")
	public FileUp(String name, File file, String uploadDirectory, String contentType) {
		this.name = name;
		this.file = file;
		this.uploadDirectory = uploadDirectory;
		this.contentType = contentType;
		if(this.contentType==null) this.contentType = TypeMap.getInstance().getContentType(file);
		
		try {
			uRLEncodedUploadName = URLEncoder.encode(uploadDirectory + file.getName(), getConfig().getCharset());
		} catch (UnsupportedEncodingException e) {
			//SimpleLogger.log("FormFileField.getURLEncodedUploadName(): UnsupportedEncodingException: "+charSet, e);
			try{
				uRLEncodedUploadName = URLEncoder.encode(uploadDirectory + file.getName(), "UTF-8");
			} catch (UnsupportedEncodingException e1) {
				//SimpleLogger.log("FormFileField.getURLEncodedUploadName(): UnsupportedEncodingException UTF-8", e);
				uRLEncodedUploadName = URLEncoder.encode(uploadDirectory + file.getName());
			}
		}
	}

	public File getFile() {
		return file;
	}

	public String getName() {
		return name;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUploadDirectory() {
		return uploadDirectory;
	}

	public String getUploadName() {
		return uploadDirectory + file.getName();
	}
	public String getURLEncodedUploadName() {
		return uRLEncodedUploadName;
	}
}
